package com.dudu.utils;

import java.io.File;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.ObjectMetadata;
import com.dudu.access.RemindUtils;

import core.util.DateUtil;

public class SsoUtil {
    private static final Logger log = core.util.Logger.getLogger(SsoUtil.class.getSimpleName());
    private static ResourceBundle resourceBundle = PropertyResourceBundle.getBundle("voicenoticeconfig");

    private static String endpoint = resourceBundle.getString("endpoint"); // 内网：oss-cn-beijing-internal.aliyuncs.com
    private static String accessKeyId = resourceBundle.getString("accessKeyId");
    private static String accessKeySecret = resourceBundle.getString("accessKeySecret");
    private static String bucketName = resourceBundle.getString("bucketName");
    private static String key = resourceBundle.getString("baseKey");

    // private static String endpoint="oss-cn-beijing.aliyuncs.com";
    // private static String accessKeyId ="LTAITfy6V4fDvGrb";
    // private static String accessKeySecret = "HNAj2vm1CN9qwUjMeXWeby0H2sk8Kc";
    // private static String bucketName="yonyou-test-01";
    // private static String key ="gaotest/";
    /**
     * 向OSS服务器存文件
     * @param mediaNameKey
     * @param fileAddress
     * @return
     */
    public String putNewFile(String mediaNameKey, String fileAddress) {
	OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
	if (null != ossClient) {
	    try {
		ossClient.putObject(bucketName, mediaNameKey, new File(fileAddress));
		log.info("上传文件成功bucketName:"+bucketName+"/"+mediaNameKey);
		return mediaNameKey;
	    } catch (Exception e) {
		log.error("上传文件失败,文件名为：" + mediaNameKey, e);
		e.printStackTrace();

	    } finally {
		ossClient.shutdown();
	    }
	}
	return "";
    }

    /**
     * 向OSS服务器上下载文件
     * @param mediaNameKey
     * @param fileAddress
     * @return
     */
    public int getNewFile(String mediaNameKey, String fileAddress) {

	OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
	if (null != ossClient) {
	    ObjectMetadata ossObject = null;
	    try {
		File file = new File(fileAddress);
		if (file.isFile()) {
		    file.delete();
		}
		// 下载object到文件

		ossObject = ossClient.getObject(new GetObjectRequest(bucketName, mediaNameKey), new File(fileAddress));
		return 1;
	    } catch (Exception e) {
		if (ossObject == null) {
		    RemindUtils.remind(mediaNameKey, "OSS未知错误" + e.getMessage() + mediaNameKey, "sms");
		} else {
		    RemindUtils.remind(mediaNameKey, ossObject.getRequestId() + "OSS未知错误" + mediaNameKey, "sms");
		}
		log.error("requestId:" + ossObject.getRequestId() + "下载文件失败,文件名为：" + mediaNameKey, e);
		log.error("requestId:" + ossObject.getRequestId() + "下载文件失败,文件名为：" + mediaNameKey + "。error:" + e.getMessage(), e);
		
		e.printStackTrace();
	    } finally {
		ossClient.shutdown();
	    }
	}
	return -1;
    }

    /**
     * 获取阿里云OSS文件全路径
     * 
     * @param mediaNameKey
     * @return
     */
    public  String getOssAbsPath(String mediaNameKey) {
	OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
	if (null != ossClient) {
	    try {
		Date now = new Date();
		Date expiration = DateUtils.afterDate(now, Calendar.DATE, 30);// 获取当前时间+30秒
		URL url = ossClient.generatePresignedUrl(bucketName, mediaNameKey, expiration);// 生成绝对地址
		if (url != null) {
		    return url.toString();
		}
	    } catch (Exception e) {
		log.error("获取下载地址失败,文件名为：" + mediaNameKey, e);
		log.error("获取下载文件失败,文件名为：" + mediaNameKey + "。error:" + e.getMessage(), e);
		e.printStackTrace();
	    } finally {
		ossClient.shutdown();
	    }
	}
	return "";
    }

    /**
     * 根据文件名称获取OSS服务器key
     * 
     * @param mediaName
     * @return
     */
    public static String getNewMediaName(String mediaName) {
	return key + getNewDirectory(mediaName) + mediaName;
    }

    /**
     * 目录获取
     * 
     * @param mediaName
     * @return
     */
    private static String getNewDirectory(String mediaName) {
	if (mediaName.length() > 14) {
	    String date = mediaName.substring(0, 14);
	    Date filedate = DateUtil.parseDate(date, "yyyyMMddHHmmss");
	    if (null != filedate) {
		return mediaName.substring(0, 6) + "/";
	    }
	}
	SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyyMM");
	String monthStr = sdfMonth.format(new Date());
	return monthStr + "/";
    }
}
